package com.yangoxix.lovegoing.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yangoxix.lovegoing.page.ActivityPage;
import com.yangoxix.lovegoing.page.UserInfo;
import com.yangoxix.lovegoing.pojo.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * The type Manage activiry service.
 *
 * @author chenzou
 * @date 2021 -09-19 16:35:41
 * @program: lovegoing
 * @description: 发布者管理活动
 * @author: chenzou
 * @create: 2021 -09-19 14:07
 */
@Service
public class ManageActiviryService {

    /**
     * Gets activiry by username.
     *
     * @param userName the user name
     * @return the activiry by username
     */
    public ArrayList<ActivityPage> getActiviryByUsername(String userName) {
        Activity activity = new Activity();
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        String username = userName;
        wrapper.eq("publisher", username);
        List<Activity> as = activity.selectList(wrapper);
        ArrayList<ActivityPage> Pages = new ArrayList<>();

        for (Activity a : as) {
//      通过名字获取img

            ActivityPage activityPage = new ActivityPage();

            String activityname = a.getActivityname();

            Image image = new Image();

            QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();

            imageQueryWrapper.eq("activityname", activityname);

            List<Image> images = image.selectList(imageQueryWrapper);


            //把全部的imgurl放入page中的list
            ArrayList<String> imagesList = new ArrayList<>();
            for (Image imagef : images) {
                imagesList.add(imagef.getImgurl());
            }
            activityPage.setImagePath(imagesList);


            String detail = a.getDetail();
//            判断是否有超过25个字如果有对其进行截取
            if (detail.length() > 25) {
                //截取前25个字符
                activityPage.setDetailCout(detail.substring(0, 25));
            } else {
                activityPage.setDetailCout(detail);
            }

//            设置标题
            activityPage.setActivityname(activityname);


            Pages.add(activityPage);
        }
        //    对数据进行封装
//    返回pagebean
        return Pages;
    }

    /**
     * Get audit user list.通过活动名从用户审核表获取活动的全部申请者
     *
     * @param activityname the activityname
     * @return the list
     * @author chenzou
     * @date 2021 -09-19 16:35:41
     */
    public List<AuditUser> getAuditUser(String activityname){
        AuditUser auditUser = new AuditUser();
        QueryWrapper<AuditUser> eq = new QueryWrapper<AuditUser>().eq("activityname", activityname);
        List<AuditUser> auditUsers = auditUser.selectList(eq);
        return auditUsers;
    }

    /**
     * Get user byname list.
     *
     * @param activityname the activityname
     * @return the list
     * @author chenzou
     * @date 2021 -09-20 09:05:41
     */
    public ArrayList<UserInfo> getUserByname(String activityname){
        UserInActivity ua = new UserInActivity();
        QueryWrapper<UserInActivity> wrapper = new QueryWrapper<>();
        wrapper.eq("activityname", activityname);
//        获取到的值：
        List<UserInActivity> uas = ua.selectList(wrapper);
        ua.getUsername();
//        操作开始：从uas获取关于username的list，利用它组装数据到userinfo，list集合起来

        return  getUserInfoByname(uas.stream().map(UserInActivity::getUsername).collect(Collectors.toList()));

    }

    public ArrayList<UserInfo> getUserInfoByname(List<String> userNames) {
        ArrayList<UserInfo> userInfos = new ArrayList<>();
        User user = new User();
        for (String userName : userNames) {
            User find = user.selectById(userName);
            UserInfo userInfo = new UserInfo();
            try {
                userInfo.setUserName(find.getUsername());
                userInfo.setMotto(find.getMotto());
                userInfo.setPotrait(find.getPhoto());
            } catch (Exception e) {
                System.out.println("没有找到已报名的用户");
                userInfo.setUserName(userName);
                userInfo.setMotto(null);
                userInfo.setPotrait(null);
            }
            userInfos.add(userInfo);
        }
        return userInfos;
    }
}
